﻿// ***********************************************************************
// Assembly         : UnitTest
// Author           : Kristian
// Created          : 05-09-2014
//
// Last Modified By : Kristian, Henrik
// Last Modified On : 05-12-2014
// ***********************************************************************
using CtrLayer;
using DBLayer.Datacontext;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ModelLayer;
using System;
using System.Linq;
using WcfService;

namespace UnitTest
{
    /// <summary>
    /// Class TestCtr contains tests for the Different Controller classes.
    /// </summary>
    [TestClass]
    public class TestWcf
    {
        private RealLifeService _wcfService;
        private RLADataContext _db;

        /// <summary>
        /// Initializes the CtrTest instance.
        /// </summary>
        [TestInitialize()]
        public void Initialize()
        {
            _wcfService = new RealLifeService();
            _db = new RLADataContext();
        }

        /// <summary>
        /// Tests the create user functionality - should success.
        /// </summary>
        [TestMethod]
        public void TestWCFCreateUser()
        {
            int uniquePart = _db.DBUsers.Max(user => user.userId) + 1;

            string email = "email" + uniquePart + "@def.com";
            var user1 = new User
            {
                Email = email,
                FirstName = "Henrik",
                LastName = "Mikkelsen",
                Gender = 'M',
                Password = "jegelskerkristian",
                ProfileName = "Hense" + uniquePart
            };
            user1.CurrentStatus = User.Status.Active;

            int createdUser = _wcfService.CreateUser(user1);

            Assert.IsTrue(createdUser != -1, "An exception was thrown from db");

            Assert.IsTrue(createdUser > 0, "A normal create scenario failed with error code: " + createdUser);

            createdUser = _wcfService.CreateUser(user1);
            Assert.IsTrue(createdUser == -2, "User created even though email was already present in db");
            user1.Email += email;

            createdUser = _wcfService.CreateUser(user1);
            Assert.IsTrue(createdUser == -3, "User created even though profile name was already present in db");
        }

        /// <summary>
        /// Tests the get user functionality - should success.
        /// </summary>
        [TestMethod]
        public void TestWCFGetUser()
        {
            DBUser refUser = _db.DBUsers.First();
            var user = _wcfService.GetUser(refUser.email, refUser.password);
            Assert.AreEqual(refUser.email, user.Email, "User wasn't found via email + password");

            string username = "lala";
            string password = "asdfasdfadf";
            user = _wcfService.GetUser(username, password);
            Assert.IsNull(user, "somehow a user was found with these stupid informations: " +
                                "username: {0}, password: {1}", username, password);
        }

        [TestMethod]
        public void TestGetAchievement()
        {
            int achId = 193;
            int userId = 474;

            UserAchievement ua = _wcfService.GetUserAchievement(userId, achId);
            Assert.IsNotNull(ua);
        }
    }
}